﻿package net.wangit.dev.unit;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.JDBCException;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.Validator;
import net.wangit.util.Checker;

public class JoinElementIdValidator implements Validator {
	private static final String SQL = "select eid from ELEMENT where uid=?  and eid=? ";

	public JoinElementIdValidator() {
	}

	public String validate(ActionContext ac, String value, String expression,
			int rowIndex) {
		String domain;
		String targetUnitId;
		boolean unchecked = !"1".equals(ac.getRequestParameter(rowIndex,
				"_s_elements"));
		if (unchecked)
			return null;
		if (Checker.isEmpty(value))
			return LabelUtil.get(ac, "system", "validation.notnull");
		domain = ConsoleDomainUtil.getDomain(ac);
		targetUnitId = JoinElementsUI.getTargetUnitId();
		if (Checker.isEmpty(domain) || Checker.isEmpty(targetUnitId)) {
			return null;
		} else {
			net.wangit.MapList list;
			DB db = DBHelper.getDomainDB(domain);
			try {
				list = db.query(JoinElementIdValidator.SQL, new String[] {
						targetUnitId, value }, new int[] { 2, 2 });
			} catch (JDBCException e) {
				return null;
			}
			if (!Checker.isEmpty(list)) {
				return LabelUtil.get(ac, "dev", "import.element_id_used");
			} else {
				return null;
			}
		}
	}

	public String validate(ActionContext ac, String from, String to, String s) {
		return null;
	}

}
